import { getImageInfo } from '@/common/request/api'
export const fullLink = (host : string, url : string) => {
	if (/(http|https):\/\/([\w.]+\/?)\S*/.test(url) || /data:image\S*/.test(url)) {
		return url
	} else {
		return host + url
	}
}

export const getAppImageInfo = (src : string) : Promise<UniNamespace.GetImageInfoSuccessData> => {
	return new Promise(async (resolve, reject) => {
		// #ifdef MP-WEIXIN||MP-ALIPAY
		try {
			let {
				data
			} = await getImageInfo(src)
			src = data
		} catch (e) {

		}
		// #endif
		uni.getImageInfo({
			src,
			success: res => {
				resolve(res)
			},
			fail: err => {
				reject(err)
			}
		})
	})
}

export const saveImage = (src : string) => {
	return new Promise((resolve, reject) => {
		// #ifdef H5
		const a = document.createElement('a');
		a.href = src;
		a.setAttribute('download', 'chart-download');
		a.click();
		resolve({})
		// #endif
		// #ifndef H5
		uni.saveImageToPhotosAlbum({
			filePath: src,
			success: () => {
				resolve({})
			},
			fail: err => {
				reject(err)
			}
		});
		// #endif

	})
}